from django.utils import timezone
import random

class GameManager:
    @staticmethod
    def calculate_damage(attacker, defender):
        base_damage = max(1, attacker.attack - defender.defense)
        # 添加随机浮动
        variation = random.uniform(0.8, 1.2)
        return int(base_damage * variation)
    
    @staticmethod
    def process_battle_turn(attacker, defender):
        damage = GameManager.calculate_damage(attacker, defender)
        defender.current_hp -= damage
        return {
            'damage': damage,
            'is_critical': False,  # 后续可以添加暴击系统
            'remaining_hp': defender.current_hp
        }
    
    @staticmethod
    def check_battle_end(player_hp, monster_hp):
        if player_hp <= 0:
            return 'defeat'
        elif monster_hp <= 0:
            return 'victory'
        return None
    
    @staticmethod
    def process_battle_rewards(character, monster, session):
        # 基础金币奖励
        gold_reward = monster.reward_gold
        # 根据楼层增加奖励
        floor_bonus = session.current_floor * 0.1
        total_gold = int(gold_reward * (1 + floor_bonus))
        
        character.gold += total_gold
        character.save()
        
        return {
            'gold': total_gold,
            'exp': monster.reward_exp,
        }
    
    @staticmethod
    def process_rewards(character, monster):
        character.gold += monster.reward_gold
        character.save()
    
    @staticmethod
    def level_up_check(character):
        # 实现升级逻辑
        pass
    
    @staticmethod
    def generate_floor_rewards(floor):
        # 根据楼层生成奖励
        pass 